package com.yugao.fintech.draper.admin.controller;

import com.yugao.fintech.draper.admin.service.DictService;
import com.yugao.fintech.draper.admin.param.DictDTO;
import com.yugao.fintech.draper.admin.param.DictItemDTO;
import com.yugao.fintech.draper.core.model.ResultVO;
import com.yugao.fintech.draper.log.annotation.AuditLog;
import com.yugao.fintech.draper.log.enums.BusinessType;
import com.yugao.fintech.draper.datasource.entity.PageResult;
import com.yugao.fintech.draper.admin.param.DictQuery;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dict")
@RequiredArgsConstructor
@Tag(name = "系统字典管理")
public class DictController {
    private final DictService dictService;

    @Operation(summary = "查询系统字典分页列表")
    @GetMapping(value = "/page")
    public ResultVO<PageResult<DictDTO>> pageQuery(@RequestBody DictQuery dictQuery) {
        return ResultVO.success(PageResult.success(dictService.pageQuery(dictQuery)));
    }

    @AuditLog(value = "字典管理", businessType = BusinessType.INSERT)
    @Operation(summary = "添加字典")
    @PostMapping(value = "/add")
    public ResultVO<Boolean> add(@RequestBody DictDTO dictDTO) {
        return ResultVO.success(dictService.addDict(dictDTO));
    }

    @AuditLog(value = "字典管理", businessType = BusinessType.UPDATE)
    @Operation(summary = "修改字典")
    @PostMapping(value = "/update")
    public ResultVO<Boolean> update(@RequestBody DictDTO dictDTO) {
        return ResultVO.success(dictService.updateDict(dictDTO));
    }

    @AuditLog(value = "字典管理", businessType = BusinessType.DELETE)
    @Operation(summary = "删除字典")
    @DeleteMapping(value = "/delete")
    public ResultVO<Boolean> delete(Long id) {
        return ResultVO.success(dictService.deleteDict(id));
    }

    @Operation(summary = "获取字典详细信息")
    @GetMapping(value = "/findById")
    public ResultVO<DictDTO> findById(Long id) {
        return ResultVO.success(dictService.findById(id));
    }

    @Operation(summary = "获取全部字典")
    @AuditLog(value = "字典管理", businessType = BusinessType.OTHER)
    @GetMapping(value = "/findAll")
    public ResultVO<List<DictDTO>> findAll() {
        return ResultVO.success(dictService.findAll());
    }

    @Operation(summary = "根据字典类型获取字典下的所有字典项")
    @GetMapping("/getDictItemsByType")
    public ResultVO<List<DictItemDTO>> getDictItemsByType(String type) {
        return ResultVO.success(dictService.getDictItemsByType(type));
    }
}